<?php
/* 
* @Author: LDF QQ 47121862
* @Date:   2014-06-02 17:21:40
* @Last Modified by:   LDF QQ 47121862
* @Last Modified time: 2014-06-12 21:22:09
*/
class ArticleModel extends viewModel{
	public $table='article';
	public $view=array(
		'info'=>array(
			'type'=>INNER_JOIN,
			'on'=>'article.aid=info.aid'
		),
		'tip'=>array(
			'type'=>INNER_JOIN,
			'on'=>'article.tid=tip.tid'
		),
		'category'=>array(
			'type'=>INNER_JOIN,
			'on'=>'article.cid=category.cid'
		)
	);
	public $validate=array(
		array('title','nonull','标题不能为空',2,3),
		array('cid','nonull','请选择分类',2,3)
	);
	//添加文章
	function addArt(){
		if($this->create()){
			$this->data['addtime']=time();
			$this->data['uid']=$_SESSION['user']['uid']; //用户id
			$this->data['uname']=$_SESSION['user']['nickname'];
			if(isset($this->data['flag']))
				$this->data['flag']=implode(',', $this->data['flag']);
			$this->data['cont']=mb_substr($this->content, 0,180,'utf8').'...';
			$this->pic();
			if(isset($_POST['huodong'])) unset($this->data['slide']); //作为活动就删除slide
			$aid=$this->add();
			if($aid){
				//添加内容到文章关联表 自动取$_POST值
				$_POST['aid']=$aid;
				M('info')->add();
				return $aid;
			}else{
				$this->error='添加文章失败';
				return false;
			}
		}else{
			$this->error='验证未通过';
			return false;
		}
	}
	/**
	 * 修改文章
	 * 联合info表进行操作。
	 * @return [type] [description]
	 */
	function editArt(){
		if( !empty($_FILES['pic']['name']) || !empty($_FILES['big']['name']) ) $this->pic(); //处理图片
		if(isset($_POST['huodong'])) unset($_POST['slide']); //作为活动就删除slide
		return $this->save($_POST)?1:0;
	}
	/**
	 * 删除文章
	 * 删除相关图片 small medium pic big
	 * 删除article,info,comment,enjoy
	 * @return [type] [description]
	 */
	function delArt(){
		$aid=Q('aid',0,'intval'); $oaid=array('aid'=>$aid);
		$data = M('article')->find($aid);
		$this->delOldPic($data);
		return (M('comment')->del($oaid)&&M('enjoy')->del($oaid)&&M('info')->del($oaid)&&M('article')->del($aid))?1:0;
	}

	//处理上传图片
	function pic(){
		// 首页列表 300*200  轮播 1180*500  右侧列表 120*80  |活动列表图380*200 活动幻灯片900*350
		$u=new Upload();
		$i=new Image();
		if(!empty($_FILES['pic']['name'])){ //普通图片处理
			//修改进来 则 删除原有图片
			if(isset($_POST['oldpic'])) @unlink(ROOT_PATH.$_POST['oldpic']); //删除原有图片
			if(isset($_POST['oldsmall'])) @unlink(ROOT_PATH.$_POST['oldsmall']); //删除原有图片
			if(isset($_POST['oldmedium'])) @unlink(ROOT_PATH.$_POST['oldmedium']); //删除原有图片
			$info=$u->upload('pic');
			if(isset($_POST['huodong'])){ //如果勾选活动
				$medium = $info[0]['dir'].$info[0]['filename'].'_380x200.'.$info[0]['ext'];
				$i->thumb($info[0]['path'],$medium,380,200);
			}else{
				$medium = $info[0]['dir'].$info[0]['filename'].'_300x200.'.$info[0]['ext'];
				$i->thumb($info[0]['path'],$medium,300,200);
			}
			$small = $info[0]['dir'].$info[0]['filename'].'_120x80.'.$info[0]['ext'];
			$i->thumb($info[0]['path'],$small,120,80);
			$this->data['small']=$small;
			$this->data['medium']=$medium;
			$this->data['pic']=$info[0]['path'];
			if(isset($_POST['oldsmall'])){ //如果是修改就写到$_POST
				$_POST['small']=$small;
				$_POST['medium']=$medium;
				$_POST['pic']=$info[0]['path'];
			}
		}
		if(!empty($_FILES['big']['name'])){ //处理幻灯片
			if(isset($_POST['oldbig'])) @unlink(ROOT_PATH.$_POST['oldbig']); //删除原有图片
			$u=new Upload();
			$i=new Image();
			$infobig=$u->upload('big');
			if(isset($_POST['huodong'])){ //如果勾选活动
				$big = $infobig[0]['dir'].$infobig[0]['filename'].'_900x350.'.$infobig[0]['ext'];
				$i->thumb($infobig[0]['path'],$big,900,350);
			}else{
				$big = $infobig[0]['dir'].$infobig[0]['filename'].'_1180x500.'.$infobig[0]['ext'];
				$i->thumb($infobig[0]['path'],$big,1180,500);
			}
			@unlink(ROOT_PATH.$infobig[0]['path']); // 删除上传原尺寸图片
			$this->data['big']=$big;
			if(isset($_POST['oldbig'])) $_POST['big']=$big; //修改就写到POST
		}
	}
	//删除相关所有图片(编辑器内图片未完成);
	function delOldPic($data){
		if(!empty($data['small'])) @unlink(ROOT_PATH.$data['small']);
		if(!empty($data['medium'])) @unlink(ROOT_PATH.$data['medium']);
		if(!empty($data['big'])) @unlink(ROOT_PATH.$data['big']);
		if(!empty($data['pic'])) @unlink(ROOT_PATH.$data['pic']);
	}
}
